package com.hhw.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.User;
import com.hhw.util.DBUtil;

public class UpdateUserDao {

	
	/*
	 * 验证该用户是否存在<根据id>
	 */
	public boolean isExist(int user_id){
		return findUsertById(user_id)==null?false:true;
	}
	
	
	public User findUsertById(int id){
		try{
			Connection con=DBUtil.getConnection();
			PreparedStatement pstm=con.prepareStatement("select * from user where user_id=?");
			pstm.setInt(1, id);
			ResultSet rs=pstm.executeQuery();
			User p=null;
			while(rs.next()){
				p=new User();
			p.setUser_id(rs.getInt(1));
			p.setUser_name(rs.getString(2));
			p.setUser_name(rs.getString(3));
			p.setUser_sex(rs.getString(4));
			p.setUser_phone(rs.getString(5));
			p.setUser_add(rs.getString(6));
				
			}
			return p;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

			
	
	/*更新用户信息*/
	public boolean updateUser(User user){
		try{
			Connection con=DBUtil.getConnection();
			PreparedStatement pstm=con.prepareStatement("update user set user_name=?,user_sex=?,user_phone=?,user_add=?  where user_id=?");
			pstm.setString(1, user.getUser_name());
			pstm.setString(2, user.getUser_sex());
			pstm.setString(3, user.getUser_phone());
			pstm.setString(4, user.getUser_add());           
			pstm.setInt(5, user.getUser_id());
			int rs = pstm.executeUpdate();
            System.out.println(rs);
            if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	
	
	
}
